package com.caothai.demo_tracnghiem;

import java.util.ArrayList;
import java.util.List;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Quanly_cauhoi extends SQLiteOpenHelper{
	private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "tracnghiem";
    private static final String ten_table = "ds_cauhoi";
 
    // danh sách các field của table ds_Cauhoi
    private static final String KEY_ID = "_id";
    private static final String KEY_NOIDUNG_CAUHOI = "noidung_cauhoi";
    private static final String KEY_NOIDUNG_TRALOI1 = "noidung_traloi1";
    private static final String KEY_NOIDUNG_TRALOI2 = "noidung_traloi2";
    private static final String KEY_NOIDUNG_TRALOI3 = "noidung_traloi3";
    private static final String KEY_NOIDUNG_TRALOI4 = "noidung_traloi4";
    private static final String KEY_DAPAN = "dapan";
    private static final String KEY_THELOAI = "theloai";
    private static final String KEY_MUCDO = "mucdo";

	public Quanly_cauhoi(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// tạo table
		String SQL_String = "CREATE TABLE " + ten_table + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
				+ KEY_NOIDUNG_CAUHOI + " TEXT,"
                + KEY_NOIDUNG_TRALOI1 + " TEXT, "
                + KEY_NOIDUNG_TRALOI2 + " TEXT, "
                + KEY_NOIDUNG_TRALOI3 + " TEXT, "
                + KEY_NOIDUNG_TRALOI4 + " TEXT, "
                + KEY_DAPAN + " TEXT, "
                + KEY_THELOAI + " TEXT, "
                + KEY_MUCDO + " TEXT" + ")";
        db.execSQL(SQL_String);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
		// Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + ten_table);
 
        // Create tables again
        onCreate(db);	
 	}

	// xem het cau hoi
	public ArrayList<Cauhoi> getAllCauhoi() {
	  	ArrayList<Cauhoi> CauhoiList = new ArrayList<Cauhoi>();
	  	String[] ds_dieukien_loc=null; 
	    
	  	String selectQuery = "SELECT  * FROM " + ten_table;
	 
	    SQLiteDatabase db = this.getReadableDatabase();
	    Cursor cursor = db.rawQuery(selectQuery, ds_dieukien_loc);
	    
	 
	    if (cursor.moveToFirst()) {
	        do {
	            Cauhoi Cauhoi = new Cauhoi();
	            Cauhoi.set_id(Integer.parseInt(cursor.getString(0)));
	            Cauhoi.setNoidung_cauhoi(cursor.getString(1));
	            Cauhoi.setNoidung_traloi1(cursor.getString(2));
	            Cauhoi.setNoidung_traloi2(cursor.getString(3));
	            Cauhoi.setNoidung_traloi3(cursor.getString(4));
	            Cauhoi.setNoidung_traloi4(cursor.getString(5));
	            Cauhoi.setDapan(cursor.getString(6));
	            Cauhoi.setTheloai(cursor.getString(7));
	            Cauhoi.setMucdo(cursor.getString(8));
	            // Them  Cauhoi vào list
	            CauhoiList.add(Cauhoi);
	        } while (cursor.moveToNext());
	    }
	    db.close();
	    return CauhoiList;	
}
	// lay ngau nhien n cau hoi
	public ArrayList<Cauhoi> get_ngaunhien_n_Cauhoi(int n) {
	  	ArrayList<Cauhoi> CauhoiList = new ArrayList<Cauhoi>();
	  	String[] ds_dieukien_loc=null; 
	    
	  	String selectQuery = "SELECT  * FROM " + ten_table + " order by random() limit "+n;
	 
	    SQLiteDatabase db = this.getReadableDatabase();
	    Cursor cursor = db.rawQuery(selectQuery, ds_dieukien_loc);
	    
	 
	    if (cursor.moveToFirst()) {
	        do {
	            Cauhoi Cauhoi = new Cauhoi();
	            Cauhoi.set_id(Integer.parseInt(cursor.getString(0)));
	            Cauhoi.setNoidung_cauhoi(cursor.getString(1));
	            Cauhoi.setNoidung_traloi1(cursor.getString(2));
	            Cauhoi.setNoidung_traloi2(cursor.getString(3));
	            Cauhoi.setNoidung_traloi3(cursor.getString(4));
	            Cauhoi.setNoidung_traloi4(cursor.getString(5));
	            Cauhoi.setDapan(cursor.getString(6));
	            Cauhoi.setTheloai(cursor.getString(7));
	            Cauhoi.setMucdo(cursor.getString(8));
	            // Them  Cauhoi vào list
	            CauhoiList.add(Cauhoi);
	        } while (cursor.moveToNext());
	    }
	    db.close();
	    return CauhoiList;	
}

	// them 1 cau hoi moi
	public void them_Cauhoi(Cauhoi Cauhoi) {
		 SQLiteDatabase db = this.getWritableDatabase();
		 String nullColumnHack=null; // chấp nhận field có giá trị null
		 ContentValues values = new ContentValues();
		 if (Cauhoi.get_id()!=-1) values.put(KEY_ID,Cauhoi.get_id());
		 values.put(KEY_NOIDUNG_CAUHOI, Cauhoi.getNoidung_cauhoi()); 
		 values.put(KEY_NOIDUNG_TRALOI1, Cauhoi.getNoidung_traloi1()); 
		 values.put(KEY_NOIDUNG_TRALOI2, Cauhoi.getNoidung_traloi2()); 
		 values.put(KEY_NOIDUNG_TRALOI3, Cauhoi.getNoidung_traloi3()); 
		 values.put(KEY_NOIDUNG_TRALOI4, Cauhoi.getNoidung_traloi4()); 
		 values.put(KEY_DAPAN, Cauhoi.getDapan()); 
		 values.put(KEY_THELOAI, Cauhoi.getTheloai()); 
		 values.put(KEY_MUCDO, Cauhoi.getMucdo()); 

		 db.insert(ten_table, nullColumnHack, values);
		 db.close(); 		
	}
	 
	// xoa tat ca cau hoi
	public void xoatatca_Cauhoi() {
		 SQLiteDatabase db = this.getWritableDatabase();
		 String whereClause="";
		 String[] whereArgs=null;
		 db.delete(ten_table, whereClause, whereArgs);		  	
		 db.close(); 		
	}
	 
	public void xoa_1_Cauhoi(int id){
		SQLiteDatabase db= this.getWritableDatabase();
		db.execSQL("delete from  " + ten_table + " where _id="+id);
		db.close();
	}

	public int sua_1_Cauhoi(Cauhoi Cauhoi) { 
		 SQLiteDatabase db = this.getWritableDatabase(); 
		 ContentValues values = new ContentValues(); 
		 values.put(KEY_NOIDUNG_CAUHOI, Cauhoi.getNoidung_cauhoi()); 
		 values.put(KEY_NOIDUNG_TRALOI1, Cauhoi.getNoidung_traloi1()); 
		 values.put(KEY_NOIDUNG_TRALOI2, Cauhoi.getNoidung_traloi2()); 
		 values.put(KEY_NOIDUNG_TRALOI3, Cauhoi.getNoidung_traloi3()); 
		 values.put(KEY_NOIDUNG_TRALOI4, Cauhoi.getNoidung_traloi4()); 
		 values.put(KEY_DAPAN, Cauhoi.getDapan()); 
		 values.put(KEY_THELOAI, Cauhoi.getTheloai()); 
		 values.put(KEY_MUCDO, Cauhoi.getMucdo()); 

		 String whereClause=KEY_ID + "=?"; 
		 String[] whereArgs={String.valueOf(Cauhoi.get_id())}; 
		 return db.update(ten_table, values, whereClause, whereArgs); 
		} 

}
